package com.jhyspringcloud.order.service.impl;

import com.jhyspringcloud.config.pojo.dto.OrderDto;
import com.jhyspringcloud.config.pojo.dto.UserDto;
import com.jhyspringcloud.feign.client.UserClient;
import com.jhyspringcloud.order.dao.OrderDao;
import com.jhyspringcloud.order.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Service
public class OderServiceImpl implements OrderService {

    @Resource
    OrderDao orderDao;

    @Resource
    RestTemplate restTemplate;

    @Resource
    UserClient userClient;

//    @Override
//    public OrderDto findOrderById(Long id) {
//        OrderDto orderDto = new OrderDto();
//        BeanUtils.copyProperties(orderDao.selectByPrimaryKey(id),orderDto);
//
//        //根据userid 获取user信息
//        if(null != orderDto.getUserId()){
//            String url = "http://userservice/user/";
//           UserEntity userEntity =  restTemplate.getForObject(url+orderDto.getUserId().toString(), UserEntity.class);
//           if(null != userEntity){
//               UserDto ud = new UserDto();
//               BeanUtils.copyProperties(userEntity,ud);
//               orderDto.setUserDto(ud);
//           }
//        }
//        return orderDto;
//    }


    /**
     * @author: robert
     * @description: 使用userclient
     * @date: 2021/9/10 10:11
     * @return
     */
    @Override
    public OrderDto findOrderById(Long id) {
        OrderDto orderDto = new OrderDto();
        BeanUtils.copyProperties(orderDao.selectByPrimaryKey(id),orderDto);


        //根据userid 获取user信息
        if(null != orderDto.getUserId()){
            UserDto userDto = userClient.getUserById(orderDto.getUserId());
            orderDto.setUserDto(userDto);
        }
        return orderDto;
    }




}
